import 'package:date_format/date_format.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:fwanandroid/model/api/WanAndroidService.dart';
import 'package:fwanandroid/model/bean/Article.dart';
import 'package:fwanandroid/model/bean/SystemParent.dart';

class SystemPage extends StatefulWidget {
  const SystemPage({super.key});

  @override
  State<SystemPage> createState() => _SystemPageState();
}

class _SystemPageState extends State<SystemPage> with AutomaticKeepAliveClientMixin{
  late EasyRefreshController _controller;
  List<SystemParent> _articles = [];

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController(
        controlFinishLoad: true, controlFinishRefresh: true);
    loadArticles();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: EasyRefresh.builder(
            controller: _controller,
            header: const MaterialHeader(),
            footer: const ClassicFooter(),
            onRefresh: () async {
              loadArticles();
            },
            childBuilder: (context, physics) {
              return CustomScrollView(
                physics: physics,
                slivers: [
                  SliverList(
                      delegate: SliverChildBuilderDelegate((context, index) {
                    return buildArticleItem(context, index);
                  }, childCount: _articles.length))
                ],
              );
            }),
      ),
    );
  }

  Widget buildArticleItem(context, index) {
    SystemParent article = _articles[index];
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "${article.name}",
            style: TextStyle(fontSize: 16,color: Colors.black),
          ),
          SizedBox(
            height: 8,
          ),
          Wrap(
            spacing: 8,
            direction: Axis.horizontal,children:
            article.children.map((item){
              return Text(item.name,style: TextStyle(fontSize: 12,color: Colors.black));
            }).toList()
          ,),
        ],
      ),
    );
  }

  loadArticles() {
    WanAndroidService.getSystemTypes().then((value) {
      setState(() {
          _articles = value;
      });
      _controller.finishRefresh();
      _controller.finishLoad(IndicatorResult.noMore);
    });
  }

  @override
  bool get wantKeepAlive => true;
}
